﻿/**********************************************************************
*创建人：蔡水明
*创建时间：2018/1/5 14:24:32
*说明：<Function>
/**********************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Utilities
{
    public static class JavaScriptHelper
    {
        /// <summary>
        /// 获取JavaScript中Xpath获取node的语句
        /// </summary>
        /// <param name="xpath"></param>
        /// <returns></returns>
        public static string GetXpathJs(string xpath)
        {
            xpath = xpath.Replace("\"", "\\\"");
            return $"document.evaluate(\"{xpath}\",document,null,XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue";
        }
        /// <summary>
        /// JavaScript点击某个按钮
        /// </summary>
        /// <param name="xpath"></param>
        /// <returns></returns>
        public static string GetClickJs(string xpath,bool ById=false)
        {
            if(ById)
                return $"document.getElementById(\"{xpath}\").click();";
            else
                return $"{GetXpathJs(xpath)}.click();";
        }
        /// <summary>
        /// JavaScritp设置某个input的值
        /// </summary>
        /// <param name="xpath"></param>
        /// <returns></returns>
        public static string GetSetValueJs(string xpath,string theValue,bool ById=false)
        {
            if(ById)
                return $"document.getElementById(\"{xpath}\").value=\"{theValue}\";";
            else
                return $"{GetXpathJs(xpath)}.value=\"{theValue}\";";
        }
        /// <summary>
        /// 增加空值检查的单击
        /// </summary>
        /// <param name="xpath"></param>
        /// <param name="ById"></param>
        /// <returns></returns>
        public static string GetClickJsByCheck(string xpath, bool ById = false)
        {
            string js = "if({0}!=null) {0}.click();";
            string elem;
            if (ById)            
                elem = $"document.getElementById(\"{xpath}\")";
            else            
                elem = GetXpathJs(xpath);
            return string.Format(js, elem);
        }
        /// <summary>
        /// 获取滚动屏幕的js代码
        /// </summary>
        /// <returns></returns>
        public static string GetScrollScreen()
        {
            return "var h = document.documentElement.scrollHeight || document.body.scrollHeight;window.scrollTo(h, h); ";
        }
        /// <summary>
        /// 从文件中读取用于判断是否滚动条在底部的js代码
        /// </summary>
        private static string CheckIsScrollBottomJs = string.Empty;
        /// <summary>
        /// 获取判断是否滚动到底部的js代码
        /// </summary>
        /// <returns></returns>
        public static string GetIsScrollBottom()
        {
            if (string.IsNullOrEmpty(CheckIsScrollBottomJs))
            {
                string filename = Path.Combine(Environment.CurrentDirectory, "Cfg", "CheckIsScrollBottom.js");
                CheckIsScrollBottomJs = LogHelper.FileToString(filename);
                if (string.IsNullOrEmpty(CheckIsScrollBottomJs))
                    LogHelper.Error("查找判断是否滚动条在底部的js文件失败！");
            }
            return CheckIsScrollBottomJs;
        }
        /// <summary>
        /// 判断加好友请求是否还有更多
        /// </summary>
        /// <returns></returns>
        public static string CheckFriendRequestMore()
        {
            return "document.getElementsByClassName(\"pam uiBoxLightblue _5cz uiMorePagerPrimary\")==null;";
        }    
    }
}
